using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using Microsoft.Phone.Info;
using System;
namespace MicroMsg.Scene
{
	public class NetSceneAuth : NetSceneBaseEx<AuthRequest, AuthResponse, AuthRequest.Builder>
	{
		public const int STATUS_ENABLE = 1;
		public const int STATUS_NOTIFY = 2;
		private const string TAG = "NetSceneAuth";
		public static string mAuthUserName;
		public static string mAuthUserPwdMD5;
		public static string mAuthUserPwd2MD5;
		public bool doScene(string userName, string string_0)
		{
			if (userName == "facebook@wechat_auth")
			{
				return this.doSceneEx(userName, string_0, "", "", "");
			}
			return this.doSceneEx(userName, Util.getCutPasswordMD5(string_0), Util.getFullPasswordMD5(string_0), "", "");
		}
		public bool doSceneWithVerify(string userName, string md5Pwd, string md5Pwd2, string imgSid, string imgCode)
		{
			return this.doSceneEx(userName, md5Pwd, md5Pwd2, imgSid, imgCode);
		}
		private bool doSceneEx(string userName, string string_0, string pwd2, string imgSid, string imgCode)
		{
			base.beginBuilder();
			NetSceneAuth.mAuthUserName = userName;
			NetSceneAuth.mAuthUserPwdMD5 = string_0;
			NetSceneAuth.mAuthUserPwd2MD5 = pwd2;
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(1);
			this.mBuilder.UserName = Util.toSKString(userName);
			this.mBuilder.Pwd = Util.toSKString(string_0);
			this.mBuilder.Pwd2 = Util.NullAsNil(pwd2);
			this.mBuilder.ImgSid = Util.toSKString(imgSid);
			this.mBuilder.ImgCode = Util.toSKString(imgCode);
			this.mBuilder.Channel = AppInfoHelper.ChannelId;
			this.mBuilder.Language = AccountMgr.GetCurrentLanguage();
			this.mBuilder.DeviceBrand = DeviceStatus.get_DeviceManufacturer();
			this.mBuilder.DeviceModel = DeviceStatus.get_DeviceName();
			this.mBuilder.OSType = Environment.get_OSVersion().get_Version().ToString();
			this.mBuilder.TimeZone = Util.getTimeZoneOffsetGwt();
			this.mSessionPack.mCmdID = 1;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(AuthRequest request, AuthResponse response)
		{
			EventCenter.postEvent(EventConst.ON_NETSCENE_AUTH_ERR, -800000, null);
		}
		protected override void onSuccess(AuthRequest request, AuthResponse response)
		{
			int uin = (int)response.Uin;
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (uin != 0 && (ret == RetConst.MM_OK || ret == RetConst.MM_ERR_CRITICALUPDATE || ret == RetConst.MM_ERR_RECOMMENDEDUPDATE))
			{
				Log.smethod_5("NetSceneAuth", "auth scene success.");
				AccountMgr.onLogin(response.UserName.String);
				HostService.updateAuthBuiltinIP(response.BuiltinIPList);
				this.updateUserInfo(request, response, SessionPackMgr.getSeverID());
				EventCenter.postEvent(EventConst.ON_NETSCENE_AUTH_SUCCESS, response, null);
				return;
			}
			Log.smethod_1("NetSceneAuth", "auth scene failed, ret =" + ret);
			if (ret == RetConst.MM_ERR_NEEDREG)
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_AUTH_NEEDREG, null, null);
				return;
			}
			if (ret != RetConst.MM_ERR_NEED_VERIFY)
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_AUTH_ERR, ret, null);
				return;
			}
			if (response.ImgBuf != null && response.ImgSid != null)
			{
				VerifyCodeArgs verifyCodeArgs = new VerifyCodeArgs();
				verifyCodeArgs.mImageSid = response.ImgSid.String;
				verifyCodeArgs.mImageBuf = response.ImgBuf.Buffer.ToByteArray();
				Log.smethod_5("NetSceneAuth", "received verify image , sid = " + verifyCodeArgs.mImageSid);
				EventCenter.postEvent(EventConst.ON_NETSCENE_AUTH_NEEDVERIFY, verifyCodeArgs, null);
				return;
			}
			Log.smethod_1("NetSceneAuth", "NEED_VERIFY_USER, but ImgSid or ImgBuf is null");
			EventCenter.postEvent(EventConst.ON_NETSCENE_AUTH_ERR, ret, null);
		}
		private bool updateUserInfo(AuthRequest authRequest, AuthResponse authResponse, byte[] serverID)
		{
			Account curAccount = AccountMgr.getCurAccount();
			curAccount.bytesSessionkey = authResponse.SessionKey.ToByteArray();
			curAccount.bytesServerID = serverID;
			if (authRequest.UserName.String == "facebook@wechat_auth")
			{
				curAccount.strPwd = authResponse.Password;
				curAccount.strPwd2 = authResponse.Password;
			}
			else
			{
				curAccount.strPwd = authRequest.Pwd.String;
				curAccount.strPwd2 = authRequest.Pwd2;
			}
			curAccount.nUin = authResponse.Uin;
			curAccount.strUsrName = authResponse.UserName.String;
			curAccount.strNickName = authResponse.NickName.String;
			curAccount.strBindEmail = authResponse.BindEmail.String;
			if (!string.IsNullOrEmpty(authResponse.BindMobile.String))
			{
				curAccount.strBindMobile = authResponse.BindMobile.String;
			}
			curAccount.nBindQQ = authResponse.BindUin;
			curAccount.nStatus = authResponse.Status;
			curAccount.strOfficalNickName = authResponse.OfficialNickName.String;
			curAccount.strOfficalUserName = authResponse.OfficialUserName.String;
			curAccount.nPushMailStatus = (int)authResponse.PushMailStatus;
			curAccount.strQQMicroBlog = authResponse.QQMicroBlogUserName.String;
			curAccount.nQQMBlogStatus = (int)authResponse.QQMicroBlogStatus;
			curAccount.dbLastSessionKeyTimeStamp = Util.getNowSeconds();
			curAccount.strAlias = authResponse.Alias;
			curAccount.nPluginFlag = authResponse.PluginFlag;
			curAccount.bytesA2Key = authResponse.A2Key.Buffer.ToByteArray();
			curAccount.strFSURL = authResponse.FSURL;
			AccountMgr.updateAccount();
			return true;
		}
	}
}
